using Coldairarrow.Business.Base_Manage;
using Coldairarrow.Util;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Coldairarrow.Api.Controllers.PlatForm
{
  [Route("/SubApp/[controller]/[action]")]
  public class SubAppController : BaseApiController
  {

    readonly IPermissionBusiness _permissionBus;
    readonly IBase_UserBusiness _userBus;
    #region DI
    public SubAppController(IPermissionBusiness permissionBus,
                          IBase_UserBusiness userBus)
    {
      _permissionBus = permissionBus;
      _userBus = userBus;
    }
    #endregion


    [HttpPost]
    [NoCheckJWT]
    public async Task<List<Base_ActionDTO>> GetOperatorMenuList([FromQuery]string token)
    {
      if (!JWTHelper.CheckToken(token, JWTHelper.JWTSecret))
      {
        throw new Exception("token校验失败！");
      }
      var payload = JWTHelper.GetPayload<JWTPayload>(token);
      if (payload.Expire < DateTime.Now)
      {
        throw new Exception("token过期！");
      }
      var user = await _userBus.GetTheDataAsync(payload.UserId);
      if (user == null)
      {
        throw new Exception("用户不存在！");
      }
      return await _permissionBus.GetUserMenuListAsync(payload.UserId);
    }
  }
  
}
